class Solution1 {
    //两两比较
    public static String longestCommonPrefix(String[] strs) {
        if(strs.length == 1){
            return strs[0];
        }
        String ret = "";
        for (int i = 0; i < strs.length - 1; i++) {
            ret = bijiao(strs[i], strs[i + 1]);
            strs[i + 1] = ret;
        }
        return ret;
    }

    //先解决两个字符串最长公共前缀
    private static String bijiao(String str1, String str2) {
        StringBuilder stringBuilder = new StringBuilder();
        //用指针
        for (int i = 0; i < Math.min(str1.length(), str2.length()); i++) {
            if (str1.charAt(i) != str2.charAt(i)) {
                return stringBuilder.toString();
            }
            stringBuilder.append(str1.charAt(i));
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"cir","car"};
        String s = Solution1.longestCommonPrefix(strs);
        System.out.println(s);
    }
}